package com.base.myproject.client.HR.holiday;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class VacationConverter {
	private Map<String, String> convertMap = new HashMap<String, String>();
	private static VacationConverter converter = new VacationConverter();
	private Iterator<String> keySet;
	
	private VacationConverter() {
		initMap();
	}
	
	public static VacationConverter getConverter() {
		return converter;
	}
	
	private void initMap() {
		convertMap.put("如果", "when");
		convertMap.put("则", "then");
		convertMap.put("从", "between");
		convertMap.put("到", "and");
		convertMap.put("余下", "else");
		convertMap.put("工作年限", "work_experience");
		convertMap.put("请假天数", "dates");
		convertMap.put("大于", ">");
		convertMap.put("小于", "<");
		convertMap.put("或者", "or");
		convertMap.put("并且", "and");
		convertMap.put("等于", "=");
		convertMap.put("不等于", "!=");
		
		addSalaryItemToMap();
	}
	
	private void addSalaryItemToMap() {
		String sql = "select distinct hr_salary_item.code,hr_salary_item.desc_cn from bcode_Allowance, hr_salary_item where  no_ = code union select distinct b.code, b.desc_cn from dbo.FixedExtPloy a, hr_salary_item b where a.code = b.code";
		final GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(sql, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				for (int i=0; i<result.getRowCount(); i++) {
					convertMap.put(result.getValue(i, "desc_cn"), result.getValue(i, "code"));
				}
			}
		});
	}
	
	public String convert(String text) {
		String replaceString = "";
		keySet = convertMap.keySet().iterator();
		
		while (keySet.hasNext()) {
			replaceString = keySet.next();
			text = text.replaceAll(replaceString, convertMap.get(replaceString));
		}
		
		return text;
	}
}
